﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Remoting
{
    class CacheRemotingConfiguration
    {
        CacheRemotingSetting defaultSetting = new CacheRemotingSetting();

        List<CacheRemotingSetting> settings = new List<CacheRemotingSetting>();

        protected CacheRemotingSetting Add(string settingName)
        {
            if (string.IsNullOrEmpty(settingName))
                return null;

            return Add(settingName, -1);
        }
        protected CacheRemotingSetting Add(string settingName, int timeSpan)
        {
            if (string.IsNullOrEmpty(settingName))
                return null;

            return Add(settingName, timeSpan, RemotingCacheMode.ShallowCache);
        }
        protected CacheRemotingSetting Add(string settingName, int timeSpan, RemotingCacheMode mode)
        {
            if (string.IsNullOrEmpty(settingName))
                return null;

            CacheRemotingSetting setting = new CacheRemotingSetting();

            setting.SettingName = settingName.Trim().ToUpper();
            setting.CacheMode = mode;
            setting.TimeSpan = timeSpan;

            settings.Add(setting);

            return setting;

        }

        public CacheRemotingSetting Get(string name)
        {
            if (string.IsNullOrEmpty(name))
                return defaultSetting;

            name = name.Trim().ToUpper();

            foreach (CacheRemotingSetting setting in settings)
            {
                if (setting.SettingName.Equals(name, StringComparison.OrdinalIgnoreCase))
                    return setting;
            }

            CacheRemotingSetting nsetting = new CacheRemotingSetting(name);

            settings.Add(nsetting);

            return nsetting;
        }

        public void Clear()
        {
            settings.Clear();
        }
    }
}
